package cts.com.dbmanager;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendSMS {
	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
	private static final String SMTP_PORT = "2703";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

	public SendSMS() {
	}

	// create an account on ipipi.com with the given username and password
	public void msgsend() {
		String username = "Tuhin1990"; // Your Credentials
		String password = "nokia3110c";
		String smtphost = "ipipi.com";// Ip/Name of Server
		String compression = "None"; // I dont want any compression
		String from = "santuc1990@gmail.com";// ur userid@ipipi.com
		// This mobile number need not be registered with ipipi.com
		String to = "8892573226";// mobile number where u want to send sms
		String body = "Hi This Msg is sent through Java Code";
		Transport tr = null;

		try {
			Properties props = System.getProperties();
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.host", SMTP_HOST_NAME);
			props.put("mail.smtp.auth", "true");
			props.put("mail.debug", "true");
			props.put("mail.smtp.port", SMTP_PORT);
			props.put("mail.smtp.socketFactory.port", SMTP_PORT);
			props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
			props.put("mail.smtp.socketFactory.fallback", "false");

			// Get a Session object
			Session mailSession = Session.getDefaultInstance(props, null);

			// construct the message
			Message msg = new MimeMessage(mailSession);

			// Set message attributes
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] address = { new InternetAddress(to) };
			msg.setRecipients(Message.RecipientType.TO, address);
			msg.setSubject(compression);
			msg.setText(body);
			msg.setSentDate(new Date());

			tr = mailSession.getTransport("smtp");
			// try to connect
			tr.connect(smtphost, username, password);
			msg.saveChanges();
			// send msg to all recipients
			tr.sendMessage(msg, msg.getAllRecipients());
			tr.close();
		} catch (Exception e) {
			System.out.println(e.getLocalizedMessage());
		}
	}

	public static void main(String[] argv) {
		SendSMS sms = new SendSMS();
		sms.msgsend();
		System.out.println("Successfull");
	}
}